package com.tuanzi.loan.web.bean.system;

import com.google.common.collect.Lists;
import com.tuanzi.loan.business.entity.system.Advertise;
import com.tuanzi.loan.business.service.system.AdvertiseService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.web.configuration.JSFInfoBean;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;
import java.util.List;

import static com.tuanzi.loan.business.constants.DictionaryCode.ADS_PLATFORM_TYPE;

/**
 * @author Ray 2017/09/21
 */
@Data
@Debug
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class AdvertiseInfoBean extends JSFInfoBean<Advertise> {

    private static final long serialVersionUID = -2152778554108785342L;
    @Autowired
    private AdvertiseService advertiseService;

    @PostConstruct
    private void init() {
        String ids = getParamFromRequest("id");
        if (StringUtils.isNotBlank(ids)) {
            Long id = Long.valueOf(ids);
            $ = advertiseService.findOne(id);
        } else {
            $ = new Advertise();
            changeStatusToAdd();
        }
    }

    public void onSave() {
        $ = advertiseService.saveOrUpdate($);
        changeStatusToView();
        addSuccessfullyMessage();
    }
    public void onCopy() {
        $.setId(null);
        changeStatusToEdit();
    }
    @Override
    protected List<String> getDictionaryCodes() {
        return Lists.newArrayList(ADS_PLATFORM_TYPE);
    }

}
